// 烟花颜色
const colors = [
    '#ff5e62', '#ff9966', '#ffcc00', '#00cc99', 
    '#00ccff', '#9966ff', '#ff66cc', '#ff5050',
    '#66ff66', '#66ccff', '#ffcc66', '#cc99ff'
];

// 获取Canvas元素和上下文
const canvas = document.getElementById('fireworksCanvas');
const ctx = canvas.getContext('2d');

// 音频元素
const backgroundMusic = document.getElementById('backgroundMusic');
const fireworkSound = document.getElementById('fireworkSound');
const audioControl = document.getElementById('audioControl');

// 设置初始音量
backgroundMusic.volume = 0.5;
fireworkSound.volume = 0.3;

// 设置音频控制按钮事件
let isMuted = false;
audioControl.addEventListener('click', function() {
    isMuted = !isMuted;
    backgroundMusic.muted = isMuted;
    fireworkSound.muted = isMuted;
    
    this.innerHTML = isMuted ? 
        '<i class="fas fa-volume-mute"></i>' : 
        '<i class="fas fa-volume-up"></i>';
});

// 设置Canvas大小为窗口大小
function resizeCanvas() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
}

// 初始化Canvas大小
resizeCanvas();
window.addEventListener('resize', resizeCanvas);

// 烟花计数器
let fireworkCounter = 0;
const fireworkCountEl = document.getElementById('fireworkCount');
const loadingIndicator = document.getElementById('loadingIndicator');
const loadingStatus = document.getElementById('loadingStatus');
const progressBar = document.getElementById('progressBar');
const hintBox = document.getElementById('hintBox');
const textNotification = document.getElementById('textNotification');
const buttonContainer = document.getElementById('buttonContainer');
const daysTogether = document.getElementById('daysTogether');

// 资源状态元素
const musicStatus = document.getElementById('musicStatus');
const soundStatus = document.getElementById('soundStatus');
const imageStatus = document.getElementById('imageStatus');
const textStatus = document.getElementById('textStatus');

// 更新资源状态显示
function updateResourceStatus(element, status) {
    element.classList.remove('local', 'network', 'failed');
    switch(status) {
        case 'local': element.classList.add('local'); break;
        case 'network': element.classList.add('network'); break;
        case 'failed': element.classList.add('failed'); break;
    }
}

// 相识日期（2024年3月12日）
const startDate = new Date(2024, 2, 12);

// 计算相识天数
function calculateDaysTogether() {
    const today = new Date();
    const timeDiff = today.getTime() - startDate.getTime();
    const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
    daysTogether.textContent = `${days}`;
    return days;
}

// 加载暖心文字
let warmTexts = [];
async function loadWarmTexts() {
    // 尝试从本地存储加载自定义文本
    const customTexts = localStorage.getItem('customWarmTexts');
    if (customTexts) {
        warmTexts = JSON.parse(customTexts);
        updateResourceStatus(textStatus, 'local');
        return { success: true, message: '自定义暖心文字加载成功' };
    }
    
    try {
        const response = await fetch('text/warm_texts.txt');
        if (response.ok) {
            const text = await response.text();
            warmTexts = text.split('\n').filter(line => line.trim() !== '');
            updateResourceStatus(textStatus, 'local');
            return { success: true, message: '本地暖心文字加载成功' };
        }
    } catch (error) {
        console.log('本地文字加载失败，使用内置文字');
    }
    
    // 内置文字
    warmTexts = [
        "累了歇会儿，别硬撑呀，不然下一秒可能就变成'葛优躺'啦",
        "愿我们携手走过更多美好时光",
        "每一天都因你而更加精彩",
        "心怀希望，未来可期",
        "你是独一无二的存在",
        "生活因你而美好",
        "坚持梦想，终会发光",
        "微笑面对每一天",
        "你值得拥有最好的",
        "困难只是暂时的",
        "相信自己的力量",
        "阳光总在风雨后",
        "爱自己，是终身浪漫的开始",
        "保持热爱，奔赴山海",
        "你的努力终将开花结果",
        "世界因你而不同",
        "每一天都是生命的礼物",
        "坚持做对的事，时间会给你答案",
        "愿你被爱包围，幸福常在",
        "保持善良，保持努力",
        "最美的风景在路上",
        "未来的路，希望能一路同行",
        "皱眉头啦？笑一笑好看,不然皱纹都要被你挤出来开会啦",
        "走,看场电影放松下,不去当观众,难道要在家当'沙发土豆'吗？",
        "今天辛苦啦,喝口水吧,不然嘴巴都要干成'撒哈拉沙漠'啦",
        "别急别慌,我陪你弄,我可是你专属的'万能小助手'哦",
        "方案不急,思路清晰更重要,别让脑袋变成'浆糊搅拌机'啦",
        "肩膀绷太紧？揉揉会好些,来,让我给你来个'超级马杀鸡'",
        "星星还亮着,天塌不下来,就算塌下来也有我这个'人肉垫子'在呢",
        "放下手机,我们出去走走啦,不然眼睛都要变成'电子屏幕监控器'啦",
        "这事儿过了,请你吃大餐,咱们去把肚子撑成个'移动的小仓库'",
        "我可能不擅长说情话，但会用行动把'我喜欢你'写进每一天",
        "遇见你后，我的生活从黑白默片变成了4K全彩电影"
    ];
    
    updateResourceStatus(textStatus, 'network');
    return { success: true, message: '内置暖心文字加载成功' };
}

// 加载音频资源
async function loadAudio() {
    let musicLoaded = false;
    let soundLoaded = false;
    
    // 尝试加载本地背景音乐
    try {
        backgroundMusic.src = 'music/background.mp3';
        await new Promise((resolve, reject) => {
            backgroundMusic.oncanplaythrough = resolve;
            backgroundMusic.onerror = () => reject(new Error('本地音乐加载失败'));
        });
        musicLoaded = true;
        updateResourceStatus(musicStatus, 'local');
        console.log('本地背景音乐加载成功');
    } catch (localError) {
        console.log('本地背景音乐加载失败', localError);
    }

    // 尝试加载本地音效
    try {
        fireworkSound.src = 'music/firework.mp3';
        await new Promise((resolve, reject) => {
            fireworkSound.oncanplaythrough = resolve;
            fireworkSound.onerror = () => reject(new Error('本地音效加载失败'));
        });
        soundLoaded = true;
        updateResourceStatus(soundStatus, 'local');
        console.log('本地音效加载成功');
    } catch (localError) {
        console.log('本地音效加载失败', localError);
    }
    
    // 如果本地资源加载失败，尝试网络资源
    if (!musicLoaded) {
        try {
            backgroundMusic.src = 'https://lv-sycdn.kuwo.cn/48ae1d1f191ec4759cc4b767aab290ed/68686938/resource/30106/trackmedia/M800003Lb4Lr3PvfUe.mp3?filename=ambient-piano-amp-strings-123908.mp3';
            await new Promise((resolve, reject) => {
                backgroundMusic.oncanplaythrough = resolve;
                backgroundMusic.onerror = () => reject(new Error('音乐加载失败'));
            });
            musicLoaded = true;
            updateResourceStatus(musicStatus, 'network');
        } catch (e) {
            console.log('网络背景音乐加载失败');
            updateResourceStatus(musicStatus, 'failed');
        }
    }
    
    if (!soundLoaded) {
        try {
            fireworkSound.src = 'https://music.jsbaidu.com/upload/rank/20220114/c45c93138cae5c09826157f1854bf1fd.mp3';
            await new Promise((resolve, reject) => {
                fireworkSound.oncanplaythrough = resolve;
                fireworkSound.onerror = () => reject(new Error('音效加载失败'));
            });
            soundLoaded = true;
            updateResourceStatus(soundStatus, 'network');
        } catch (e) {
            console.log('网络音效加载失败');
            updateResourceStatus(soundStatus, 'failed');
        }
    }
    
    return { music: musicLoaded, sound: soundLoaded };
}

// 加载图片
async function loadImages() {
    const tvImages = document.querySelectorAll('.tv-image');
    let loadedCount = 0;
    let usedCustom = false;
    let usedNetwork = false;
    
    // 尝试从本地存储加载自定义图片
    for (let i = 0; i < 5; i++) {
        const customImage = localStorage.getItem(`customImage${i}`);
        if (customImage) {
            tvImages[i].style.backgroundImage = `url('${customImage}')`;
            loadedCount++;
            usedCustom = true;
        }
    }
    
    if (loadedCount === 5) {
        updateResourceStatus(imageStatus, 'local');
        return { success: true, message: '自定义图片加载成功' };
    }
    
    // 图片资源
    const imageSources = [
        ['image/image1.jpg', 'https://images.unsplash.com/photo-1501854140801-50d01698950b?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=400'],
        ['image/image2.jpg', 'https://images.unsplash.com/photo-1447752875215-b2761acb3c5d?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=400'],
        ['image/image3.jpg', 'https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=400'],
        ['image/image4.jpg', 'https://images.unsplash.com/photo-1476820865390-c52aeebb9891?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=400'],
        ['image/image5.jpg', 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=400']
    ];
    
    // 默认图片
    const defaultImage = "https://images.unsplash.com/photo-1506744038136-46273834b3fb?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=300&w=400";
    
    for (let i = 0; i < 5; i++) {
        // 如果这个位置已经有自定义图片，跳过
        if (localStorage.getItem(`customImage${i}`)) continue;
        
        let loaded = false;
        
        // 尝试加载本地图片
        try {
            await loadImageWithTimeout(imageSources[i][0], 1500);
            tvImages[i].style.backgroundImage = `url('${imageSources[i][0]}')`;
            loaded = true;
            loadedCount++;
        } catch (error) {
            console.log(`本地图片 ${i+1} 加载失败`);
        }
        
        // 本地加载失败，尝试网络图片
        if (!loaded) {
            try {
                await loadImageWithTimeout(imageSources[i][1], 1500);
                tvImages[i].style.backgroundImage = `url('${imageSources[i][1]}')`;
                loaded = true;
                loadedCount++;
                usedNetwork = true;
            } catch (error) {
                console.log(`网络图片 ${i+1} 加载失败`);
            }
        }
        
        // 如果都失败，使用默认图片
        if (!loaded) {
            tvImages[i].style.backgroundImage = `url('${defaultImage}')`;
        }
    }
    
    // 更新图片状态
    if (usedCustom) {
        updateResourceStatus(imageStatus, 'local');
        return { success: true, message: '图片加载成功（自定义+默认）' };
    } else if (loadedCount === 4 && !usedNetwork) {
        updateResourceStatus(imageStatus, 'local');
        return { success: true, message: '图片加载成功（本地）' };
    } else if (loadedCount === 4 && usedNetwork) {
        updateResourceStatus(imageStatus, 'network');
        return { success: true, message: '图片加载成功（网络）' };
    } else {
        updateResourceStatus(imageStatus, 'failed');
        return { success: false, message: `${4 - loadedCount}张图片加载失败，使用默认图片` };
    }
}

// 带超时的图片加载
function loadImageWithTimeout(src, timeout) {
    return new Promise((resolve, reject) => {
        const img = new Image();
        const timer = setTimeout(() => {
            img.onload = null;
            img.onerror = null;
            reject(new Error('图片加载超时'));
        }, timeout);
        
        img.onload = () => {
            clearTimeout(timer);
            resolve();
        };
        
        img.onerror = () => {
            clearTimeout(timer);
            reject(new Error('图片加载失败'));
        };
        
        img.src = src;
    });
}

// 初始化资源加载
async function initResources() {
    loadingIndicator.style.display = 'block';
    let statusMessages = [];
    let loadedCount = 0;
    const totalResources = 4;
    
    // 更新进度条
    function updateProgress() {
        loadedCount++;
        const percent = Math.min(100, Math.round((loadedCount / totalResources) * 100));
        progressBar.style.width = `${percent}%`;
    }
    
    try {
        // 并行加载资源
        const promises = [
            loadWarmTexts().then(result => {
                statusMessages.push(result.message);
                updateProgress();
            }),
            loadAudio().then(result => {
                statusMessages.push(result.music ? '音频加载成功' : '背景音乐加载失败');
                statusMessages.push(result.sound ? '音效加载成功' : '音效加载失败');
                updateProgress();
                updateProgress(); // 音频包含两个资源
            }),
            loadImages().then(result => {
                statusMessages.push(result.message);
                updateProgress();
            })
        ];
        
        // 设置5秒超时
        await Promise.race([
            Promise.all(promises),
            new Promise(resolve => setTimeout(resolve, 5000))
        ]);
        
        loadingStatus.textContent = statusMessages.join('\n');
        loadingStatus.style.color = '#66ff66';
    } catch (error) {
        console.error('资源加载错误:', error);
        loadingStatus.textContent = '资源加载完成，部分资源可能未加载成功';
        loadingStatus.style.color = '#ffcc00';
    } finally {
        // 延迟隐藏加载指示器
        setTimeout(() => {
            loadingIndicator.style.display = 'none';
            
            // 在用户交互后开始播放音乐
            document.addEventListener('click', function startMusic() {
                try {
                    backgroundMusic.play().catch(e => {
                        console.log('需要用户交互才能播放音频');
                        // 重试播放
                        setTimeout(() => backgroundMusic.play(), 1000);
                    });
                    document.removeEventListener('click', startMusic);
                } catch (e) {
                    console.log('音频播放错误:', e);
                }
            }, { once: true });
            
            // 显示提示框，5秒后自动隐藏
            setTimeout(() => {
                hintBox.classList.add('hidden');
            }, 5000);
        }, 1000);
    }
}

// 烟花类
class Firework {
    constructor() {
        this.reset();
    }
    
    reset() {
        this.x = Math.random() * canvas.width;
        this.y = canvas.height;
        this.targetY = Math.random() * canvas.height * 0.4 + 50;
        this.currentX = this.x;
        this.currentY = this.y;
        this.speed = 2 + Math.random() * 3;
        this.size = Math.random() * 2 + 1;
        this.color = colors[Math.floor(Math.random() * colors.length)];
        this.particles = [];
        this.state = 0;
    }
    
    update() {
        if (this.state === 0) {
            this.currentY -= this.speed;
            if (this.currentY <= this.targetY) {
                this.explode();
                fireworkCounter++;
                fireworkCountEl.textContent = fireworkCounter;
            }
        } else {
            for (let i = 0; i < this.particles.length; i++) {
                this.particles[i].update();
                if (this.particles[i].alpha <= 0) {
                    this.particles.splice(i, 1);
                    i--;
                }
            }
            if (this.particles.length === 0) {
                this.reset();
            }
        }
    }
    
    explode() {
        this.state = 1;
        try {
            fireworkSound.currentTime = 0;
            fireworkSound.play().catch(e => console.log('音效播放失败:', e));
        } catch (e) {
            console.log('音效播放错误:', e);
        }
        
        const particleCount = Math.floor(Math.random() * 200) + 150;
        for (let i = 0; i < particleCount; i++) {
            const angle = Math.random() * Math.PI * 2;
            const speed = Math.random() * 6 + 2;
            const vx = Math.cos(angle) * speed;
            const vy = Math.sin(angle) * speed;
            const size = Math.random() * 2 + 1;
            const color = colors[Math.floor(Math.random() * colors.length)];
            
            this.particles.push({
                x: this.currentX,
                y: this.currentY,
                vx: vx,
                vy: vy,
                size: size,
                color: color,
                alpha: 1,
                gravity: 0.05,
                friction: 0.97,
                update: function() {
                    this.x += this.vx;
                    this.y += this.vy;
                    this.vy += this.gravity;
                    this.vx *= this.friction;
                    this.vy *= this.friction;
                    this.alpha -= 0.008;
                }
            });
        }
    }
    
    draw() {
        if (this.state === 0) {
            ctx.beginPath();
            ctx.arc(this.currentX, this.currentY, this.size, 0, Math.PI * 2);
            ctx.fillStyle = this.color;
            ctx.fill();
            
            ctx.beginPath();
            ctx.moveTo(this.x, canvas.height);
            ctx.lineTo(this.currentX, this.currentY);
            ctx.strokeStyle = this.color;
            ctx.lineWidth = 1;
            ctx.stroke();
        } else {
            for (let i = 0; i < this.particles.length; i++) {
                const p = this.particles[i];
                ctx.beginPath();
                ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
                ctx.fillStyle = `rgba(${parseInt(p.color.slice(1, 3), 16)}, ${parseInt(p.color.slice(3, 5), 16)}, ${parseInt(p.color.slice(5, 7), 16)}, ${p.alpha})`;
                ctx.fill();
            }
        }
    }
}

// 创建烟花数组
const fireworks = [];
const fireworkCount = 20;
let fireworkInterval = 80;

// 初始化烟花
for (let i = 0; i < fireworkCount; i++) {
    fireworks.push(new Firework());
    setTimeout(() => {}, i * fireworkInterval);
}

// 飘屏文字管理
class FloatingTextManager {
    constructor() {
        this.texts = [];
        this.container = document.getElementById('floatingTexts');
        this.lastTextTime = 0;
        // 文字频率等级 (0-5)
        this.textFrequencyLevel = 3;
        // 文字速度等级 (0-5)
        this.speedLevel = 3;
        // 记录上一个频率等级
        this.lastFrequencyLevel = 3;
    }
    
    // 根据频率等级获取间隔时间
    getFrequencyInterval() {
        // 频率等级对应的间隔时间（毫秒）
        const intervals = [
            Number.MAX_SAFE_INTEGER, // 等级0：不显示
            1000, // 等级1：每1秒一个
            500, // 等级2：每0.5秒一个
            250, // 等级3：每0.25秒一个
            150,  // 等级4：每0.15秒一个
            50   // 等级5：每0.05秒一个
        ];
        return intervals[this.textFrequencyLevel];
    }
    
    // 根据速度等级获取动画持续时间
    getSpeedDuration() {
        // 速度等级对应的动画持续时间（秒）
        const durations = [
            9999, // 等级0：几乎不动
            20,   // 等级1：20秒
            10,   // 等级2：10秒
            5,   // 等级3：5秒
            2,   // 等级4：2秒
            1    // 等级5：1秒
        ];
        return durations[this.speedLevel];
    }
    
    addText() {
        if (warmTexts.length === 0 || this.textFrequencyLevel === 0) return;
        
        const text = document.createElement('div');
        text.className = 'floating-text';
        const textIndex = Math.floor(Math.random() * warmTexts.length);
        text.textContent = warmTexts[textIndex];
        const top = Math.random() * (canvas.height - 100) + 50;
        text.style.top = `${top}px`;
        const size = Math.floor(Math.random() * 4) + 14;
        text.style.fontSize = `${size}px`;
        text.style.color = colors[Math.floor(Math.random() * colors.length)];
        
        // 根据速度等级设置动画持续时间
        const duration = this.getSpeedDuration();
        text.style.animationDuration = `${duration}s`;
        
        this.container.appendChild(text);
        
        setTimeout(() => {
            if (text.parentNode) {
                text.parentNode.removeChild(text);
            }
        }, duration * 1000);
    }
    
    // 清除所有漂浮文字
    clearAllTexts() {
        while (this.container.firstChild) {
            this.container.removeChild(this.container.firstChild);
        }
    }
    
    update() {
        const now = Date.now();
        const interval = this.getFrequencyInterval();
        
        // 当频率等级从0变为非0时，重置计时器
        if (this.lastFrequencyLevel === 0 && this.textFrequencyLevel > 0) {
            this.lastTextTime = now;
        }
        
        if (now - this.lastTextTime > interval) {
            this.addText();
            this.lastTextTime = now;
        }
        
        this.lastFrequencyLevel = this.textFrequencyLevel;
    }
}

// 创建飘屏文字管理器
const textManager = new FloatingTextManager();

// 动画循环
function animate() {
    ctx.fillStyle = 'rgba(10, 5, 30, 0.1)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    
    for (let i = 0; i < fireworks.length; i++) {
        fireworks[i].update();
        fireworks[i].draw();
    }
    
    textManager.update();
    requestAnimationFrame(animate);
}

// 启动动画
function startAnimation() {
    animate();
    setTimeout(() => {
        for (let i = 0; i < 8; i++) {
            setTimeout(() => {
                textManager.addText();
            }, i * 600);
        }
    }, 1000);
}

// 控制面板事件
const fireworkFrequency = document.getElementById('fireworkFrequency');
const textFrequency = document.getElementById('textFrequency');
const textSpeed = document.getElementById('textSpeed');
const musicVolume = document.getElementById('musicVolume');
const soundVolume = document.getElementById('soundVolume');
const panelButton = document.getElementById('panelButton');
const tvButton = document.getElementById('tvButton');
const controlPanel = document.getElementById('controlPanel');
const tvBox = document.getElementById('tvBox');

fireworkFrequency.addEventListener('input', function() {
    fireworkInterval = parseInt(this.value);
});

// 文字频率控制 (0-5级)
textFrequency.addEventListener('input', function() {
    const newLevel = parseInt(this.value);
    
    // 当从0变为非0时，清除所有文字
    if (textManager.textFrequencyLevel === 0 && newLevel > 0) {
        textManager.clearAllTexts();
        
        // 显示通知
        textNotification.classList.remove('hidden');
        setTimeout(() => {
            textNotification.classList.add('hidden');
        }, 2000);
    }
    
    textManager.textFrequencyLevel = newLevel;
});

// 文字速度控制 (0-5级)
textSpeed.addEventListener('input', function() {
    const newLevel = parseInt(this.value);
    
    // 当速度改变时，清除所有文字
    if (textManager.speedLevel !== newLevel) {
        textManager.clearAllTexts();
        
        // 显示通知
        textNotification.classList.remove('hidden');
        setTimeout(() => {
            textNotification.classList.add('hidden');
        }, 2000);
    }
    
    textManager.speedLevel = newLevel;
});

musicVolume.addEventListener('input', function() {
    backgroundMusic.volume = this.value / 100;
});

soundVolume.addEventListener('input', function() {
    fireworkSound.volume = this.value / 100;
});

let panelVisible = false;
panelButton.addEventListener('click', function() {
    tvBox.classList.add('hidden');
    panelVisible = !panelVisible;
    controlPanel.classList.toggle('hidden', !panelVisible);
    this.innerHTML = panelVisible ? 
        '<i class="fas fa-times"></i> 关闭面板' : 
        '<i class="fas fa-sliders-h"></i> 控制面板';
});

let tvVisible = false;
tvButton.addEventListener('click', function() {
    controlPanel.classList.add('hidden');
    panelVisible = false;
    panelButton.innerHTML = '<i class="fas fa-sliders-h"></i> 控制面板';
    tvVisible = !tvVisible;
    tvBox.classList.toggle('hidden', !tvVisible);
    this.innerHTML = tvVisible ? 
        '<i class="fas fa-times"></i> 关闭电视' : 
        '<i class="fas fa-tv"></i> 小电视';
});

// 小电视图片轮播
function startImageCarousel() {
    const tvImages = document.querySelectorAll('.tv-image');
    let currentImageIndex = 0;
    
    function changeTvImage() {
        tvImages[currentImageIndex].classList.remove('active');
        currentImageIndex = (currentImageIndex + 1) % tvImages.length;
        tvImages[currentImageIndex].classList.add('active');
    }
    
    setInterval(changeTvImage, 4000);
}

// 更新时间
function updateTime() {
    const now = new Date();
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    const seconds = now.getSeconds().toString().padStart(2, '0');
    document.getElementById('currentTime').textContent = `${hours}:${minutes}:${seconds}`;
}

// 模拟天气数据
function updateWeather() {
    const weatherTypes = ["晴", "多云", "小雨", "阴天", "阵雨"];
    const weather = weatherTypes[Math.floor(Math.random() * weatherTypes.length)];
    const temp = Math.floor(Math.random() * 15) + 20;
    document.getElementById('weather').textContent = `${weather} ${temp}°C`;
}

// 三击事件处理
function setupTripleClick() {
    let clickCount = 0;
    let lastClickTime = 0;
    const tripleClickThreshold = 300; // 300ms内三次点击
    
    document.addEventListener('click', (e) => {
        // 排除按钮和控制面板区域
        if (e.target.closest('.button-container, .control-panel, .tv-box, .top-controls')) {
            return;
        }
        
        const now = Date.now();
        
        if (now - lastClickTime > tripleClickThreshold) {
            clickCount = 1;
        } else {
            clickCount++;
        }
        
        lastClickTime = now;
        
        if (clickCount === 3) {
            // 切换按钮容器显示状态
            buttonContainer.classList.toggle('hidden');
            
            // 如果按钮显示出来，5秒后自动隐藏
            if (!buttonContainer.classList.contains('hidden')) {
                setTimeout(() => {
                    buttonContainer.classList.add('hidden');
                }, 5000);
            }
            
            clickCount = 0;
        }
    });
}

// 文件上传处理
function setupFileUploads() {
    const applyUploadsButton = document.getElementById('applyUploadsButton');
    const bgMusicInput = document.getElementById('backgroundMusicUpload');
    const soundInput = document.getElementById('fireworkSoundUpload');
    const imageInput = document.getElementById('imageUpload');
    const textInput = document.getElementById('textUpload');
    
    const musicStatusEl = document.getElementById('musicUploadStatus');
    const soundStatusEl = document.getElementById('soundUploadStatus');
    const imageStatusEl = document.getElementById('imageUploadStatus');
    const textStatusEl = document.getElementById('textUploadStatus');
    
    // 应用所有上传
    applyUploadsButton.addEventListener('click', function() {
        // 应用背景音乐
        if (bgMusicInput.files.length > 0) {
            const file = bgMusicInput.files[0];
            if (file.type.startsWith('audio/')) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const objectURL = URL.createObjectURL(file);
                    backgroundMusic.src = objectURL;
                    backgroundMusic.load();
                    
                    // 存储到localStorage
                    localStorage.setItem('customBackgroundMusic', objectURL);
                    
                    musicStatusEl.textContent = `已应用: ${file.name}`;
                    updateResourceStatus(musicStatus, 'local');
                    
                    // 尝试播放新音乐
                    backgroundMusic.play().catch(e => {
                        console.log('需要用户交互才能播放音频');
                    });
                };
                reader.readAsDataURL(file);
            } else {
                musicStatusEl.textContent = '请选择音频文件';
            }
        }
        
        // 应用音效
        if (soundInput.files.length > 0) {
            const file = soundInput.files[0];
            if (file.type.startsWith('audio/')) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const objectURL = URL.createObjectURL(file);
                    fireworkSound.src = objectURL;
                    fireworkSound.load();
                    
                    // 存储到localStorage
                    localStorage.setItem('customFireworkSound', objectURL);
                    
                    soundStatusEl.textContent = `已应用: ${file.name}`;
                    updateResourceStatus(soundStatus, 'local');
                };
                reader.readAsDataURL(file);
            } else {
                soundStatusEl.textContent = '请选择音频文件';
            }
        }
        
        // 应用图片
        if (imageInput.files.length > 0) {
            const files = Array.from(imageInput.files);
            const validImages = files.filter(file => file.type.startsWith('image/'));
            const maxImages = Math.min(validImages.length, 4);
            
            for (let i = 0; i < maxImages; i++) {
                const file = validImages[i];
                const reader = new FileReader();
                reader.onload = function(e) {
                    const dataURL = e.target.result;
                    // 存储到localStorage
                    localStorage.setItem(`customImage${i}`, dataURL);
                    
                    // 设置到电视图片
                    const tvImages = document.querySelectorAll('.tv-image');
                    tvImages[i].style.backgroundImage = `url('${dataURL}')`;
                };
                reader.readAsDataURL(file);
            }
            
            imageStatusEl.textContent = `已应用 ${maxImages} 张图片`;
            updateResourceStatus(imageStatus, 'local');
        }
        
        // 应用文字
        if (textInput.files.length > 0) {
            const file = textInput.files[0];
            if (file.type === 'text/plain' || file.name.endsWith('.txt')) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    warmTexts = e.target.result.split('\n').filter(line => line.trim() !== '');
                    
                    // 存储到localStorage
                    localStorage.setItem('customWarmTexts', JSON.stringify(warmTexts));
                    
                    textStatusEl.textContent = `已应用: ${file.name}`;
                    updateResourceStatus(textStatus, 'local');
                    
                    // 清除现有文字并重新开始
                    textManager.clearAllTexts();
                    textManager.lastTextTime = 0;
                };
                reader.readAsText(file);
            } else {
                textStatusEl.textContent = '请选择文本文件(.txt)';
            }
        }
        
        // 显示成功通知
        textNotification.textContent = "资源已成功应用并保存";
        textNotification.classList.remove('hidden');
        setTimeout(() => {
            textNotification.classList.add('hidden');
        }, 2000);
    });
}

// 初始化应用
function init() {
    calculateDaysTogether();
    setInterval(updateTime, 1000);
    setInterval(updateWeather, 30000);
    setInterval(calculateDaysTogether, 60000);
    
    initResources().then(() => {
        startAnimation();
        startImageCarousel();
        updateWeather();
        setupTripleClick();
        setupFileUploads();
    });
}

window.addEventListener('orientationchange', resizeCanvas);
init();
